﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class LabelColorTools : MonoBehaviour 
{
	[MenuItem("Tools/Label/Image && State Button/Record Start Color")]
    static void RecordStartColors()
    {
		UIChangedList.Clean();
		
		char[] trimEnd = (".prefab").ToCharArray();
		Debug.Log("开始设置默认Color:......");
		
		string fileFolder = "Assets/GameResources/UI/Dep/";
	 	string[] files = Directory.GetFiles(fileFolder, "*", SearchOption.AllDirectories);
        foreach(string file in files)
        {			
			if(file.Contains(".meta"))
				continue;
			
			Object obj = AssetDatabase.LoadMainAssetAtPath(file);
			
			if(obj is GameObject)
			{
				GameObject gameObj = obj as GameObject;
				RecordStartColor(file, file.TrimEnd(trimEnd), gameObj.transform);
			}
		}
		
		Debug.Log("设置完毕！");
		
		UIChangedList.Save();
    }
	
	static void RecordStartColor(string rootFile, string objPath, Transform trans)
	{
		bool changed = false;
		bool catchButton = false;
		if(!catchButton)
		{
			UIImageButton button = trans.GetComponent<UIImageButton>();
			if(button != null)
			{
				catchButton = true;
				if(button.label != null)
				{
					if(button.normalColor != button.label.color)
					{
						changed = true;
						button.normalColor = button.label.color;
						Debug.Log(string.Format("Reset Normal Color：{0}", objPath));
					}
				}
			}
		}
		
//		if(!catchButton)
//		{
//			UIStateButton button = trans.GetComponent<UIStateButton>();
//			if(button != null)
//			{
//				catchButton = true;
//				if(button.label != null)
//				{
//					if(button.normalColor != button.label.color)
//					{
//						changed = true;
//						button.normalColor = button.label.color;
//						Debug.Log(string.Format("Reset Normal Color：{0}", objPath));
//					}
//				}
//			}
//		}
		
		if(!catchButton)
		{		
			//子节点递归
			for(int j = 0; j < trans.childCount; j++)
			{
				Transform child = trans.GetChild(j);
				RecordStartColor(rootFile, objPath + "/" + child.name, child);
			}
		}
		else if(changed)
		{		
			EditorUtility.SetDirty(trans.gameObject);
			UIChangedList.Add(rootFile);
		}
	}
}
